# encoding=utf-8
# 载入模块
import redis
import uuid
from utils.utils import *
pool = redis.ConnectionPool(host=IP, port=6379, db=0)
pool1 = redis.ConnectionPool(host=IP, port=6379, db=1)
r = redis.Redis(connection_pool=pool)
# _r = redis.Redis(host=IP, port=6379, db=0)
# _r1 = redis.Redis(host=IP, port=6379, db=1)
_r = redis.Redis(connection_pool=pool)
_r1 = redis.Redis(connection_pool=pool1)


class RedisPub():
    def get(key):
        if _r.exists(key):
            # print("get key", key, ":", _r.get(key).decode('utf-8'))
            return str(_r.get(key).decode('utf-8'))
        else:
            return None

    def set(key, name):
        _r.set(key, str(name))
        _r.expire(key, 3600)

    def rpush(key, name):
        _r.rpush(key, str(name))
        _r.pubs

    def delete(key):
        _r.delete(key)

    def set_token(user_id):
        struuid = user_id + '-' + str(uuid.uuid4()).replace("-", "")
        _r.set(user_id, struuid)
        return struuid

    def del_tokenbyuserid(user_id):
        _r.delete(user_id)

    def get_token_byuserid(user_id):
        token = None
        if (_r.exists(user_id)):
            token = _r.get(user_id).decode('utf-8')
        return token

    def get_userid_bytoken(token):
        token_substr = token.split('-')[-1]
        user_id = token.replace('-' + token_substr, '')
        get_token = None
        if (_r.exists(user_id)):
            get_token = _r.get(user_id).decode('utf-8')
        if get_token is None or get_token == '':
            return False, None
        if token == get_token:
            return True, user_id
        return False, None


if __name__ == "__main__":
    '''
    str1 = {"093fd51a-9783-11e7-85bb-6045cb73dedb": 1}
    dict_rst = json.loads(str1)
    for (d, x) in dict_rst.items():
        print("key:" + d + ",value:" + str(x))
    '''
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1)
    r = redis.StrictRedis(connection_pool=pool)
    # while True:
    #     inputs = input("publish:")
    #     if inputs == 'over':
    #         print('停止发布')
    #         break
    #     r.publish('t_kol', inputs)
    r.publish('t_kol', '1234')